package com.bodu.depress.service.impl;

import com.bodu.core.entity.ParamReq;
import com.bodu.core.entity.enums.StatusFlagEnum;
import com.bodu.core.exception.custom.DeleteException;
import com.bodu.core.exception.custom.SaveException;
import com.bodu.core.exception.custom.SearchException;
import com.bodu.core.exception.custom.UpdateException;
import com.bodu.depress.entity.request.DepressCollectDetailRequest;
import com.bodu.depress.entity.vo.DepressCollectDetailVo;
import com.bodu.depress.entity.vo.DepressQuestionVo;
import com.bodu.depress.enums.QuestionTypeEnums;
import com.bodu.depress.mapper.DepressCollectDetailMapper;
import com.bodu.depress.entity.po.DepressCollectDetailPo;
import com.bodu.depress.service.DepressCollectDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 */
@Service("depressCollectDetailService")
public class DepressCollectDetailServiceImpl implements DepressCollectDetailService {

    @Resource
    private DepressCollectDetailMapper depressCollectDetailMapper;

    @Override
    public DepressCollectDetailVo getModel(Long id) throws SearchException {
        DepressCollectDetailVo depressCollectDetailVo = new DepressCollectDetailVo();
        DepressCollectDetailPo depressCollectDetailPo = depressCollectDetailMapper.getModel(id);
        BeanUtils.copyProperties(depressCollectDetailPo, depressCollectDetailVo);
        return depressCollectDetailVo;
    }

    @Override
    public Long save(DepressCollectDetailRequest request) throws SaveException {
        depressCollectDetailMapper.save(request);
        return request.getId();
    }

    @Override
    public Long update(DepressCollectDetailRequest request) throws UpdateException {
        depressCollectDetailMapper.update(request);
        return request.getId();
    }

    @Override
    public List<DepressCollectDetailVo> listPage(ParamReq<DepressCollectDetailRequest> t) throws SearchException {
        t.setStartIndex((t.getCurrent() - 1) * t.getSize());
        List<DepressCollectDetailVo> voList = depressCollectDetailMapper.listPage(t);
        voList.forEach(vo -> vo.setStatusFlagName(StatusFlagEnum.getStatusValues(vo.getStatusFlag())));
        return voList;
    }

    @Override
    public Integer countPage(ParamReq<DepressCollectDetailRequest> t) throws SearchException {
        return depressCollectDetailMapper.countPage(t);
    }

    @Override
    public Integer updateDelFlagById(Long id, Integer delFlag) throws DeleteException {
        return depressCollectDetailMapper.updateDelFlagById(id, delFlag);
    }

    @Override
    public List<DepressQuestionVo> findDepressQuestions(Long collectId) {
        List<DepressQuestionVo> questions = depressCollectDetailMapper.findDepressQuestions(collectId);
        questions.forEach(question -> question.setQuestionTypeName(QuestionTypeEnums.getStatusValues(question.getQuestionType())));
        return questions;
    }
}
